<?php

namespace App\Containers\AppSection\Project\Actions;

use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Moderation\Tasks\FindModerationByIdTask;
use App\Containers\AppSection\Moderation\Tasks\SendToModerationTask;
use App\Containers\AppSection\Moderation\Tasks\UpdateModerationTask;
use App\Containers\AppSection\Pledge\Tasks\CreateDDUTask;
use App\Containers\AppSection\Pledge\Tasks\CreateMovableTask;
use App\Containers\AppSection\Pledge\Tasks\CreateOtherTask;
use App\Containers\AppSection\Pledge\Tasks\CreatePledgeTask;
use App\Containers\AppSection\Pledge\Tasks\CreatePropertyTask;
use App\Containers\AppSection\Pledge\Tasks\DeletePledgesTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\Contracts\CreateLeaseTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\Contracts\CreateOwnershipTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\Contracts\CreateUsesTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\CreateApartmentTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\CreateBuildingTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\CreateHouseTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\CreateLandTask;
use App\Containers\AppSection\Pledge\Tasks\Properties\CreatePremiseTask;
use App\Containers\AppSection\Project\Tasks\FindProjectByIdTask;
use App\Containers\AppSection\Project\Tasks\UpdateCompanyTask;
use App\Containers\AppSection\Project\Tasks\UpdateConditionTask;
use App\Containers\AppSection\Project\Tasks\UpdateDocumentTask;
use App\Containers\AppSection\Project\Tasks\UpdateProjectTask;
use App\Ship\Exceptions\BadRequestException;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;

class UpdateBorrowerProjectAction extends Action
{
    public function run($id, Request $request)
    {
        $project = app(FindProjectByIdTask::class)->run($id);

        app(DeletePledgesTask::class)->thisProject($project->id)->run();

        foreach ($request->getInputByKey('pledges') as $key => $pledge) {
            $property = null;
            //Тип имущества
            switch ($request->getInputByKey("pledges.$key.property_type")) {
                case 'property':
                {
                    $contract = null;
                    if ($request->getInputByKey("pledges.$key.property.type_key") === 'premise' || $request->getInputByKey("pledges.$key.property.type_key") === 'building') {
                        $type = $request->getInputByKey("pledges.$key.property.type_key");

                        if ($request->getInputByKey("pledges.$key.property.$type.contract_type")) {
                            switch ($request->getInputByKey("pledges.$key.property.$type.contract_type")) {
                                case 'lease':
                                    $contract = app(CreateLeaseTask::class)->run([
                                        'address' => $request->getInputByKey("pledges.$key.property.$type.contract.address"),
                                        'number' => $request->getInputByKey("pledges.$key.property.$type.contract.number"),
                                        'total_area_type' => $request->getInputByKey("pledges.$key.property.$type.contract.total_area_type"),
                                        'total_area' => $request->getInputByKey("pledges.$key.property.$type.contract.total_area"),
                                        'land_category' => $request->getInputByKey("pledges.$key.property.$type.contract.land_category"),
                                        'permitted_use' => $request->getInputByKey("pledges.$key.property.$type.contract.permitted_use"),
                                        'other_information' => $request->getInputByKey("pledges.$key.property.$type.contract.other_information"),
                                        'rent_type' => $request->getInputByKey("pledges.$key.property.$type.contract.rent_type"),
                                        'rent' => $request->getInputByKey("pledges.$key.property.$type.contract.rent"),
                                        'contract_name' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_name"),
                                        'contract_date' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_date"),
                                        'contract_number' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_number"),
                                        'contract_tenant' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_tenant"),
                                        'contract_registered_date' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_registered_date"),
                                        'contract_registered_company' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_registered_company"),
                                        'contract_registered_number' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_registered_number"),
                                        'documents' => $request->getInputByKey("pledges.$key.property.$type.contract.documents")
                                    ]);
                                    break;
                                case 'ownership' :
                                    $contract = app(CreateOwnershipTask::class)->run([
                                        'address' => $request->getInputByKey("pledges.$key.property.$type.contract.address"),
                                        'number' => $request->getInputByKey("pledges.$key.property.$type.contract.number"),
                                        'total_area_type' => $request->getInputByKey("pledges.$key.property.$type.contract.total_area_type"),
                                        'total_area' => $request->getInputByKey("pledges.$key.property.$type.contract.total_area"),
                                        'land_category' => $request->getInputByKey("pledges.$key.property.$type.contract.land_category"),
                                        'permitted_use' => $request->getInputByKey("pledges.$key.property.$type.contract.permitted_use"),
                                        'certificate_number' => $request->getInputByKey("pledges.$key.property.$type.contract.certificate_number"),
                                        'certificate_date' => $request->getInputByKey("pledges.$key.property.$type.contract.certificate_date"),
                                        'extract_egrn_from' => $request->getInputByKey("pledges.$key.property.$type.contract.extract_egrn_from"),
                                        'other_information' => $request->getInputByKey("pledges.$key.property.$type.contract.other_information"),
                                        'extracts' => $request->getInputByKey("pledges.$key.property.$type.contract.extracts"),
                                        'documents' => $request->getInputByKey("pledges.$key.property.$type.contract.documents")
                                    ]);
                                    break;
                                case 'uses' :
                                    $contract = app(CreateUsesTask::class)->run([
                                        'address' => $request->getInputByKey("pledges.$key.property.$type.contract.address"),
                                        'number' => $request->getInputByKey("pledges.$key.property.$type.contract.number"),
                                        'total_area_type' => $request->getInputByKey("pledges.$key.property.$type.contract.total_area_type"),
                                        'total_area' => $request->getInputByKey("pledges.$key.property.$type.contract.total_area"),
                                        'land_category' => $request->getInputByKey("pledges.$key.property.$type.contract.land_category"),
                                        'permitted_use' => $request->getInputByKey("pledges.$key.property.$type.contract.permitted_use"),
                                        'other_information' => $request->getInputByKey("pledges.$key.property.$type.contract.other_information"),
                                        'uses_type' => $request->getInputByKey("pledges.$key.property.$type.contract.uses_type"),
                                        'uses' => $request->getInputByKey("pledges.$key.property.$type.contract.uses"),
                                        'rent_confirmed' => $request->getInputByKey("pledges.$key.property.$type.contract.rent_confirmed"),
                                        'contract_name' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_name"),
                                        'contract_date' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_date"),
                                        'contract_number' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_number"),
                                        'contract_tenant' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_tenant"),
                                        'contract_registered_date' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_registered_date"),
                                        'contract_registered_company' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_registered_company"),
                                        'contract_registered_number' => $request->getInputByKey("pledges.$key.property.$type.contract.contract_registered_number"),
                                        'order_company' => $request->getInputByKey("pledges.$key.property.$type.contract.order_company"),
                                        'order_number' => $request->getInputByKey("pledges.$key.property.$type.contract.order_number"),
                                        'order_date' => $request->getInputByKey("pledges.$key.property.$type.contract.order_date"),
                                        'documents' => $request->getInputByKey("pledges.$key.property.$type.contract.documents")
                                    ]);
                                    break;
                                default:
                                    $contract = null;
                                    break;
                            }
                            if (!empty($contract)) {
                                $contractId = $contract->id;
                            }
                        } else {
                            $contractId = null;
                        }
                    }
                    $apartmentId = $houseId = $landId = $premiseId = $buildingId = null;

                    //Тип строения
                    switch ($request->getInputByKey("pledges.$key.property.type_key")) {
                        case 'apartment':
                        {
                            $apartment = app(CreateApartmentTask::class)->run([
                                'address' => $request->getInputByKey("pledges.$key.property.apartment.address"),
                                'number' => $request->getInputByKey("pledges.$key.property.apartment.number"),
                                'total_area' => $request->getInputByKey("pledges.$key.property.apartment.total_area"),
                                'living_area' => $request->getInputByKey("pledges.$key.property.apartment.living_area"),
                                'apartment_number' => $request->getInputByKey("pledges.$key.property.apartment.apartment_number"),
                                'rooms_count' => $request->getInputByKey("pledges.$key.property.apartment.rooms_count"),
                                'floor' => $request->getInputByKey("pledges.$key.property.apartment.floor"),
                                'other_information' => $request->getInputByKey("pledges.$key.property.apartment.other_information"),
                                'certificate_number' => $request->getInputByKey("pledges.$key.property.apartment.certificate_number"),
                                'certificate_date' => $request->getInputByKey("pledges.$key.property.apartment.certificate_date"),
                                'extract_egrn_from' => $request->getInputByKey("pledges.$key.property.apartment.extract_egrn_from"),
                                'plans' => $request->getInputByKey("pledges.$key.property.apartment.plans"),
                                'extracts' => $request->getInputByKey("pledges.$key.property.apartment.extracts"),
                                'certificates' => $request->getInputByKey("pledges.$key.property.apartment.certificates"),
                                'documents' => $request->getInputByKey("pledges.$key.property.apartment.documents"),
                                'images' => $request->getInputByKey("pledges.$key.property.apartment.images")
                            ]);
                            if (!empty($apartment)) {
                                $apartmentId = $apartment->id;
                            }
                            break;
                        }
                        case 'house':
                        {
                            $house = app(CreateHouseTask::class)->run([
                                'address' => $request->getInputByKey("pledges.$key.property.house.address"),
                                'number' => $request->getInputByKey("pledges.$key.property.house.number"),
                                'total_area' => $request->getInputByKey("pledges.$key.property.house.total_area"),
                                'living_area' => $request->getInputByKey("pledges.$key.property.house.living_area"),
                                'other_information' => $request->getInputByKey("pledges.$key.property.house.other_information"),
                                'certificate_number' => $request->getInputByKey("pledges.$key.property.house.certificate_number"),
                                'certificate_date' => $request->getInputByKey("pledges.$key.property.house.certificate_date"),
                                'extract_egrn_from' => $request->getInputByKey("pledges.$key.property.house.extract_egrn_from"),
                                'plans' => $request->getInputByKey("pledges.$key.property.house.plans"),
                                'extracts' => $request->getInputByKey("pledges.$key.property.house.extracts"),
                                'certificates' => $request->getInputByKey("pledges.$key.property.house.certificates"),
                                'documents' => $request->getInputByKey("pledges.$key.property.house.documents"),
                                'images' => $request->getInputByKey("pledges.$key.property.house.images")
                            ]);
                            if (!empty($house)) {
                                $houseId = $house->id;
                            }
                        }
                        case 'land':
                        {
                            $land = app(CreateLandTask::class)->run([
                                'address' => $request->getInputByKey("pledges.$key.property.land.address"),
                                'number' => $request->getInputByKey("pledges.$key.property.land.number"),
                                'total_area_type' => $request->getInputByKey("pledges.$key.property.land.total_area_type"),
                                'total_area' => $request->getInputByKey("pledges.$key.property.land.total_area"),
                                'land_category' => $request->getInputByKey("pledges.$key.property.land.land_category"),
                                'permitted_use' => $request->getInputByKey("pledges.$key.property.land.permitted_use"),
                                'other_information' => $request->getInputByKey("pledges.$key.property.land.other_information"),
                                'certificate_number' => $request->getInputByKey("pledges.$key.property.land.certificate_number"),
                                'certificate_date' => $request->getInputByKey("pledges.$key.property.land.certificate_date"),
                                'extract_egrn_from' => $request->getInputByKey("pledges.$key.property.land.extract_egrn_from"),
                                'extracts' => $request->getInputByKey("pledges.$key.property.land.extracts"),
                                'documents' => $request->getInputByKey("pledges.$key.property.land.documents"),
                                'images' => $request->getInputByKey("pledges.$key.property.land.images")
                            ]);
                            if (!empty($land)) {
                                $landId = $land->id;
                            }
                            break;
                        }
                        case 'premise':
                        {
                            $premise = app(CreatePremiseTask::class)->run([
                                'address' => $request->getInputByKey("pledges.$key.property.premise.address"),
                                'number' => $request->getInputByKey("pledges.$key.property.premise.number"),
                                'total_area' => $request->getInputByKey("pledges.$key.property.premise.total_area"),
                                'rooms' => $request->getInputByKey("pledges.$key.property.premise.rooms"),
                                'floors' => $request->getInputByKey("pledges.$key.property.premise.floors"),
                                'floors_number' => $request->getInputByKey("pledges.$key.property.premise.floors_number"),
                                'premise_type' => $request->getInputByKey("pledges.$key.property.premise.premise_type"),
                                'residential_total_area' => $request->getInputByKey("pledges.$key.property.premise.residential_total_area"),
                                'other_information' => $request->getInputByKey("pledges.$key.property.premise.other_information"),
                                'certificate_number' => $request->getInputByKey("pledges.$key.property.premise.certificate_number"),
                                'certificate_date' => $request->getInputByKey("pledges.$key.property.premise.certificate_date"),
                                'extract_egrn_from' => $request->getInputByKey("pledges.$key.property.premise.extract_egrn_from"),
                                'plans' => $request->getInputByKey("pledges.$key.property.premise.plans"),
                                'extracts' => $request->getInputByKey("pledges.$key.property.premise.extracts"),
                                'certificates' => $request->getInputByKey("pledges.$key.property.premise.certificates"),
                                'documents' => $request->getInputByKey("pledges.$key.property.premise.documents"),
                                'images' => $request->getInputByKey("pledges.$key.property.premise.images"),
                                'contract_type' => $request->getInputByKey("pledges.$key.property.premise.contract_type"),
                                'contract_id' => $contractId
                            ]);
                            if (!empty($premise)) {
                                $premiseId = $premise->id;
                            }
                            break;
                        }
                        case 'building':
                        {
                            $building = app(CreateBuildingTask::class)->run([
                                'address' => $request->getInputByKey("pledges.$key.property.building.address"),
                                'number' => $request->getInputByKey("pledges.$key.property.building.number"),
                                'number_type' => $request->getInputByKey("pledges.$key.property.building.number_type"),
                                'total_area' => $request->getInputByKey("pledges.$key.property.building.total_area"),
                                'floors_number' => $request->getInputByKey("pledges.$key.property.building.floors_number"),
                                'object_composition' => $request->getInputByKey("pledges.$key.property.building.object_composition"),
                                'plans' => $request->getInputByKey("pledges.$key.property.building.plans"),
                                'documents' => $request->getInputByKey("pledges.$key.property.building.documents"),
                                'images' => $request->getInputByKey("pledges.$key.property.building.images"),
                                'other_information' => $request->getInputByKey("pledges.$key.property.building.other_information"),
                                'contract_type' => $request->getInputByKey("pledges.$key.property.building.contract_type"),
                                'contract_id' => $contractId
                            ]);
                            if (!empty($building)) {
                                $buildingId = $building->id;
                            }
                            break;
                        }
                        default:
                            throw new BadRequestException('Неверный тип строения!');
                    }

                    $property = app(CreatePropertyTask::class)->run([
                        'type_key' => $request->getInputByKey("pledges.$key.property.type_key"),
                        'apartment_id' => $apartmentId,
                        'house_id' => $houseId,
                        'land_id' => $landId,
                        'premise_id' => $premiseId,
                        'building_id' => $buildingId,
                    ]);
                    break;
                }
                case 'ddu':
                {
                    $property = app(CreateDDUTask::class)->run([
                        'number' => $request->getInputByKey("pledges.$key.property.number"),
                        'date' => $request->getInputByKey("pledges.$key.property.date"),
                        'developer' => $request->getInputByKey("pledges.$key.property.developer"),
                        'ogrn_developer' => $request->getInputByKey("pledges.$key.property.ogrn_developer"),
                        'registration_region' => $request->getInputByKey("pledges.$key.property.registration_region"),
                        'registration_number' => $request->getInputByKey("pledges.$key.property.registration_number"),
                        'sum' => $request->getInputByKey("pledges.$key.property.sum"),
                        'address' => $request->getInputByKey("pledges.$key.property.address"),
                        'total_area_type' => $request->getInputByKey("pledges.$key.property.total_area_type"),
                        'total_area' => $request->getInputByKey("pledges.$key.property.total_area"),
                        'floor' => $request->getInputByKey("pledges.$key.property.floor"),
                        'floors_number' => $request->getInputByKey("pledges.$key.property.floors_number"),
                        'conditional_number' => $request->getInputByKey("pledges.$key.property.conditional_number"),
                        'transfer_date' => $request->getInputByKey("pledges.$key.property.transfer_date"),
                        'other_information' => $request->getInputByKey("pledges.$key.property.other_information"),
                        'plans' => $request->getInputByKey("pledges.$key.property.plans"),
                        'documents' => $request->getInputByKey("pledges.$key.property.documents")
                    ]);
                    break;
                }
                case 'movable':
                {
                    $property = app(CreateMovableTask::class)->run([
                        'name' => $request->getInputByKey("pledges.$key.property.name"),
                        'brand_model' => $request->getInputByKey("pledges.$key.property.brand_model"),
                        'year' => $request->getInputByKey("pledges.$key.property.year"),
                        'body' => $request->getInputByKey("pledges.$key.property.body"),
                        'engine_volume' => $request->getInputByKey("pledges.$key.property.engine_volume"),
                        'engine_type' => $request->getInputByKey("pledges.$key.property.engine_type"),
                        'chassis' => $request->getInputByKey("pledges.$key.property.chassis"),
                        'body_color' => $request->getInputByKey("pledges.$key.property.body_color"),
                        'engine_model_number' => $request->getInputByKey("pledges.$key.property.engine_model_number"),
                        'engine_power' => $request->getInputByKey("pledges.$key.property.engine_power"),
                        'vin' => $request->getInputByKey("pledges.$key.property.vin"),
                        'ts_mfr' => $request->getInputByKey("pledges.$key.property.ts_mfr"),
                        'pts_date' => $request->getInputByKey("pledges.$key.property.pts_date"),
                        'pts_series_number' => $request->getInputByKey("pledges.$key.property.pts_series_number"),
                        'pts_issued_by' => $request->getInputByKey("pledges.$key.property.pts_issued_by"),
                        'pts_issued_address' => $request->getInputByKey("pledges.$key.property.pts_issued_address"),
                        'sts_date' => $request->getInputByKey("pledges.$key.property.sts_date"),
                        'sts_series_number' => $request->getInputByKey("pledges.$key.property.sts_series_number"),
                        'sts_issued_by' => $request->getInputByKey("pledges.$key.property.sts_issued_by"),
                        'gos_number' => $request->getInputByKey("pledges.$key.property.gos_number"),
                        'address' => $request->getInputByKey("pledges.$key.property.address"),
                        'other_information' => $request->getInputByKey("pledges.$key.property.other_information"),
                        'documents' => $request->getInputByKey("pledges.$key.property.documents"),
                        'pts_scans' => $request->getInputByKey("pledges.$key.property.pts_scans"),
                        'sts_scans' => $request->getInputByKey("pledges.$key.property.sts_scans"),
                        'images' => $request->getInputByKey("pledges.$key.property.images")
                    ]);
                    break;
                }
                case 'other':
                {
                    $property = app(CreateOtherTask::class)->run([
                        'name' => $request->getInputByKey("pledges.$key.property.name"),
                        'characteristics' => $request->getInputByKey("pledges.$key.property.characteristics"),
                        'address' => $request->getInputByKey("pledges.$key.property.address"),
                        'other_information' => $request->getInputByKey("pledges.$key.property.other_information"),
                        'serial_number' => $request->getInputByKey("pledges.$key.property.serial_number"),
                        'date' => $request->getInputByKey("pledges.$key.property.date"),
                        'documents' => $request->getInputByKey("pledges.$key.property.documents"),
                        'object_documents' => $request->getInputByKey("pledges.$key.property.object_documents"),
                        'images' => $request->getInputByKey("pledges.$key.property.images")
                    ]);
                    break;
                }
                default:
                {
                    throw new BadRequestException('Неверный тип залога!');
                }
            }

            app(CreatePledgeTask::class)->run([
                'project_id' => $project->id,
                'property_type' => $request->getInputByKey("pledges.$key.property_type"),
                'property_id' => $property->id,
                'rating_key' => 'b'
            ]);
        }

        //Компании
        app(UpdateCompanyTask::class)->run($project->company->id, [
            'finance_type' => $request->getInputByKey('company.finance_type'),
            'finance' => $request->getInputByKey('company.finance'),
            'structure_details' => $request->getInputByKey('company.structure_details'),
            'persons_information' => $request->getInputByKey('company.persons_information'),
            'revenue' => $request->getInputByKey('company.revenue'),
            'primary_occupation' => $request->getInputByKey('company.primary_occupation'),
            'additional_facts' => $request->getInputByKey('company.additional_facts'),
            'foundation_year' => $request->getInputByKey('company.foundation_year'),
            'no_site' => $request->getInputByKey('company.no_site'),
            'site' => $request->getInputByKey('company.site'),
            'logo_id' => $request->getInputByKey('company.logo_id'),
        ]);

        //Цели
        app(UpdateConditionTask::class)->run($project->condition->id, [
            'type_key' => $request->getInputByKey('condition.type_key'),
            'own_version' => $request->getInputByKey('condition.own_version'),
            'min' => $request->getInputByKey('condition.min'),
            'target' => $request->getInputByKey('condition.target'),
            'percent' => $request->getInputByKey('condition.percent'),
            'loan' => $request->getInputByKey('condition.loan'),
            'period' => $request->getInputByKey('condition.period'),
            'repayment_key' => $request->getInputByKey('condition.repayment_key'),
            'deffer_period' => $request->getInputByKey('condition.repayment_key') === 'deferred_annuity' ?
                $request->getInputByKey('condition.deffer_period') : null,
            'output_key' => count($request->getInputByKey('pledges')) > 0 ? 'manually' : 'automatically'
        ]);

        //Документы
        foreach ($request->getInputByKey('documents') as $key => $document) {
            app(UpdateDocumentTask::class)->run($request->getInputByKey("documents.$key.id"), [
                'files' => $request->getInputByKey("documents.$key.files")
            ]);
        }

        $moderation = app(SendToModerationTask::class)->run([
            'project_id' => $project->id,
            'stage' => 'moderator'
        ]);

        app(UpdateProjectTask::class)->run($project->id, [
            'title' => $request->getInputByKey('title'),
            'description' => $request->getInputByKey('description'),
            'is_draft' => $request->getInputByKey('is_draft'),
            'stage_key' => $request->getInputByKey('stage_key'),
            'medias' => $request->getInputByKey('medias'),
            'videos' => $request->getInputByKey('videos'),
            'businesses' => $request->getInputByKey('businesses'),
            'moderation_id' => $moderation->id,
            'moderation_status_key' => 'projects_checking',
            'moderated_at' => null
        ]);


        app(CreateLogTask::class)->run($request->profile()->id, "Редактирование проекта №{$project->id}", 'project');
        app(CreateLogTask::class)->run($request->profile()->id, "Проект №{$project->id}. Отправка на модерацию.", 'moderation', $moderation->id);
    }
}
